/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package NetListener;


import com.sun.org.apache.bcel.internal.generic.INSTANCEOF;
import jWASA.PHR;
import jWASA.Sample;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.lang.ClassNotFoundException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import samples.*;

/**
 *
 * @author gianmariospacagna
 */
public class NetListenerElaboratedData extends Thread {

    Boolean stop;
    Integer port;
    ElaboratedSampleCollection elaboratedSamples;
    ServerSocket serverSocket;

    public NetListenerElaboratedData(Integer port, ElaboratedSampleCollection samples) throws IOException {

        this.port = port;
        this.elaboratedSamples = samples;
        stop = false;
        this.serverSocket = new ServerSocket(port);
    }

    @Override
    public void run() {
        while (true) {
            while (!stop) {


                try {
                    System.out.println("Server Listening to the port " + port);
                    Socket socket;
                    socket = serverSocket.accept();
                    System.out.println("Connection accepted from " + socket.getInetAddress().getHostAddress());
                    ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                    Object obj = ois.readObject();

                    if (obj instanceof SampleBreath) {
                        SampleBreath sample = (SampleBreath) obj;
                        elaboratedSamples.getBreath().add(sample);
                        System.out.println("New SampleBreath received");
                    } else if (obj instanceof SampleHeartBeat) {
                        SampleHeartBeat sample = (SampleHeartBeat) obj;
                        elaboratedSamples.getHeartbeat().add(sample);
                        System.out.println("New " + sample.getClass().getName() + " received");
                    } else if (obj instanceof SampleHumidity) {
                        SampleHumidity sample = (SampleHumidity) obj;
                        elaboratedSamples.getHumidity().add(sample);
                        System.out.println("New " + sample.getClass().getName() + " received");
                    } else if (obj instanceof SampleLight) {
                        SampleLight sample = (SampleLight) obj;
                        elaboratedSamples.getLight().add(sample);
                        System.out.println("New " + sample.getClass().getName() + " received");
                    } else if (obj instanceof SamplePressure) {
                        SamplePressure sample = (SamplePressure) obj;
                        elaboratedSamples.getPressure().add(sample);
                        System.out.println("New " + sample.getClass().getName() + " received");
                    } else if (obj instanceof SampleTemperature) {
                        SampleTemperature sample = (SampleTemperature) obj;
                        elaboratedSamples.getTemperature().add(sample);
                        System.out.println("New " + sample.getClass().getName() + " received");
                    } else {
                        System.err.println("NetListenerelaboratedData-> Object not recognized: " + obj.toString());
                    }
                } catch (Exception e) {
                    //e.printStackTrace();
                    System.err.println("NetListener-> Error with socket");
                }
            }
        }
    }

    public void safeStop() throws IOException {
        stop = true;
        serverSocket.close();


    }

    public void safeResume() {
        stop = false;

    }
}
